;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 unwox <me@unwox.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus home services himitsu)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (gnu home services)
  #:use-module (gnu home services shepherd)
  #:use-module ((guixrus packages himitsu) #:prefix rus:)

  #:export (home-himitsu-configuration
            home-himitsu-service-type))

(define-record-type* <home-himitsu-configuration>
  home-himitsu-configuration make-home-himitsu-configuration
  home-himitsu-configuration?
  (himitsu himitsu-himitsu                     ;file-like
           (default rus:himitsu))
  (prompter-command himitsu-prompter-command   ; file-like
                    (default (file-append rus:hiprompt-gtk-py
                                          "/bin/hiprompt-gtk")))
  (prompter-display himitsu-prompter-display   ; string
            (default "wayland-0")))

(define (home-himitsu-shepherd-services config)
  (list (shepherd-service
         (provision '(himitsu))
         (documentation
          "Run himitsud, a secure secret storage daemon.")
         (start #~(make-forkexec-constructor
                   (list #$(file-append (himitsu-himitsu config)
                                        "/bin/himitsud"))
                   #:environment-variables
                   (append (default-environment-variables)
                    (list (string-append
                           "WAYLAND_DISPLAY="
                           #$(himitsu-prompter-display config))))
                   #:log-file
                   (format #f "~a/log/himitsu.log"
                           (or (getenv "XDG_STATE_HOME")
                               (format #f "~a/.local/state"
                                       (getenv "HOME"))))))
         (stop #~(make-kill-destructor)))))

(define (home-himitsu-xdg-configuration-files config)
  `(("himitsu/config.ini"
     ,(mixed-text-file "himitsu.ini"
       "[himitsud]\n"
       "prompter=" (himitsu-prompter-command config)))))

(define (home-himitsu-profile-packages config)
  (list rus:himitsu))

(define home-himitsu-service-type
  (service-type
   (name 'home-himitsu)
   (default-value (home-himitsu-configuration))
   (extensions
    (list (service-extension home-shepherd-service-type
                             home-himitsu-shepherd-services)
          (service-extension home-xdg-configuration-files-service-type
                             home-himitsu-xdg-configuration-files)
          (service-extension home-profile-service-type
                             home-himitsu-profile-packages)))
   (description
    "Run himitsud, a secure secret storage daemon. Before starting this service,
run @command{himitsu-init} to create a secure storage. Without it the service
will fail to start.")))
